package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	. "web_api_gin/controller"
	"web_api_gin/core"
	"web_api_gin/dao"
	"web_api_gin/dto"
	"web_api_gin/service"
)

var SysOptLogApi = SysOptLogController{}

type SysOptLogController struct {
}

//日志列表接口
func (c *SysOptLogController) Index(ctx *gin.Context) {
	//搜索条件
	var searchParams *dto.SysOptLogGetListReq
	if err := ctx.ShouldBind(&searchParams); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	//日志列表
	logList, total, err := dao.SysOptLog.GetList(ctx, searchParams)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, err.Error())
		return
	}
	//权限列表
	permList, err := dao.SysPermission.GetAll(ctx)
	permTreeOriginList := ([]*dto.SysPermissionTree)(nil)
	for _, permission := range permList {
		permTreeOriginList = append(permTreeOriginList, &dto.SysPermissionTree{
			SysPermission: *permission,
		})
	}
	permTreeList := service.SysPermission.BuildTree(permTreeOriginList)

	Success(ctx, gin.H{
		"logList":  logList,
		"total":    total,
		"pageNo":   searchParams.PageNo,
		"permList": permTreeList,
	})
}
